package MyArrayList;

import java.util.Arrays;

public class MyArrayList implements IList{
    private int[] array;//顺序表本质是控制数组
    private int useSize;//实际使用的空间

    private final int DEFINE_CAPACITY = 10;//默认容量

    public MyArrayList() {
        this.array = new int[DEFINE_CAPACITY];
    }

    private void grow(){
        this.array = Arrays.copyOf(this.array,this.array.length * 2);
    }

    private void Checkpos2(int pos){
        if(pos < 0 || pos > this.useSize){
            throw new PositionOutOfBoundsException("pos位置不合法");
        }
    }

    @Override
    public void add(int data) {
        if(isFull()){
            grow();
        }
        this.array[this.useSize++] = data;
    }

    @Override
    public void add(int data, int pos) {
        try {
            Checkpos2(pos);
            if(isFull()){
                grow();
            }
            for (int i = this.useSize - 1; i >= pos; i--) {
                this.array[i+1] = this.array[i];
            }
            this.array[pos] = data;
            this.useSize++;
        }catch(PositionOutOfBoundsException e){
            e.printStackTrace();
        }
    }

    @Override
    public boolean isFull() {
        return this.useSize == this.array.length;
    }


    private void isEmpty(){
        if(this.useSize == 0){
            throw new ListEmptyException("当前顺序表为空！");
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < this.useSize; i++) {
            if(toFind == this.array[i]){

                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < this.useSize; i++) {
            if(toFind == this.array[i]){

                return i;
            }
        }
        return -1;
    }

    private void Checkpos1(int pos){
        if(pos < 0 || pos >= this.useSize){
            throw new PositionOutOfBoundsException("pos位置不合法");
        }
    }

    @Override
    public int get(int pos) {
        try {
            isEmpty();
            Checkpos1(pos);
            return this.array[pos];
        }catch (PositionOutOfBoundsException e){
            e.printStackTrace();
        }catch (ListEmptyException e){
            e.printStackTrace();
        }

        return -1;
    }

    @Override
    public void set(int pos, int value) {
        if(pos == 0){
            this.array[pos] = value;
        }
        try {
            isEmpty();
            Checkpos1(pos);
            this.array[pos] = value;
        }catch (PositionOutOfBoundsException e){
            e.printStackTrace();
        }catch (ListEmptyException e){
            e.printStackTrace();
        }

    }

    @Override
    public void remove(int toRemove) {
        try {
            isEmpty();
            int pos = indexOf(toRemove);
            for (int i = pos; i < this.useSize - 1; i++) {
                this.array[i] = this.array[i + 1];
            }
            this.useSize--;
        }catch (ListEmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return this.useSize;
    }

    @Override
    public void claer() {
        //如果是引用类型，需要把每个位置都置null
//        for (int i = 0; i < this.useSize; i++) {
//            array[i] = null;
//        }

        this.useSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < this.useSize; i++) {
            System.out.print(this.array[i] + " ");
        }
        System.out.println();
    }
}
